#!/usr/bin/env echo "no"
# utf-8

# git clone or pull
# @param : dir
# @param : url
git_sync () {
  local dir=$1
  local url=$2
  [[ -d $dir && -d $dir/.git ]] && { 
    cd $dir;
    run "同步 $url $dir" "true" \
    git pull
    :; 
  } || {
    [[ -e $dir ]] && { 
      echo "$dir is exists"; 
      exit 1; 
    }
    run "同步 $url $dir" "true" \
    git clone $url $dir
  }
}

# 把指定文件从base目录拷贝到home目录
# @param file or dir
# @base $W
base_to_home () {
  local f=${1}
  local d=$(dirname $f)
  [ -f $W/base/$f ] && { 
    run "同步脚本 $W/base/$f" "true" \
    mkdir -p ~/$d \&\& cat $W/base/$f \> ~/$f 
  } || {
    [ -d $W/base/$f ] && {
      for sd in `ls $W/base/$f`; do
        base_to_home $f/$sd
      done
    } || :;
  }
}


ex  () { eval \$"$1"; }
var () { eval echo \$"$1"; }

# rsync local source to host target
# @param : host
# @param : source
# @param : target
rsync_to () {
  local ME=`hostname -s`
  # 如果rsync自己会报错
  [ "$ME" == "$1" ] && return 0 
  ssh -p $SSH_PORT $1 "mkdir -p $3"; 
  rsync -a --exclude=.svn --exclude=.git --exclude=logs $2 -e "ssh -p $SSH_PORT" $1:$3;
}

# same local to host path 
# @param : host
# @param : path 
same_to () {
  [ !  -e $2 ] && return 1;
  dir=`cd $2/..;pwd`; 
  rsync_to $1 $2 $dir;
}

# 修改XML文件 
# @param : xmlfile
# @param : name
# @param : value
xml_set () {
  sed -r "/<name>$2<\/name>/{ n; s#<value>.*</value>#<value>$3</value>#; }" -i $1;
}

# Checks if a variable is set to "y" or "yes".
option_enabled () {
  local VAR="$1"
  local VAR_VALUE=$(eval echo \$$VAR)
  if [[ "$VAR_VALUE" == "y" ]] || 
    [[  "$VAR_VALUE" == "yes" ]]; then
    return 0
  else
    return 1
  fi
}

now () {
  echo $(date +%s)
}

elapsed () {
  START="$1"
  STOP="$2"

  echo $(( STOP - START ))
}

start_watch () {
  START_WATCH=`now`
}

stop_watch () {
  if has_value START_WATCH
  then
    STOP_WATCH=`now`
    elapsed "$START_WATCH" "$STOP_WATCH"
  else
    msg "Start time not set... (start_watch)"
  fi
}


# die #? "abc"
#die () {
#  local -r err_code="$1"
#  local -r err_msg="$2"
#  local -r err_caller="${3:-$(caller 0)}"
#
#  msg_fail "ERROR: $err_msg"
#  msg_fail "ERROR: At line $err_caller"
#  msg_fail "ERROR: Error code = $err_code"
#  exit "$err_code"
#} >&2
#
#die_if_false () {
#  local -r err_code=$1
#  local -r err_msg=$2
#  local -r err_caller=$(caller 0)
#
#  if [[ "$err_code" != "0" ]]
#  then
#    die $err_code "$err_msg" "$err_caller"
#  fi
#} >&2
#
#die_if_true () {
#  local -r err_code=$1
#  local -r err_msg=$2
#  local -r err_caller=$(caller 0)
#
#  if [[ "$err_code" == "0" ]]
#  then
#    die $err_code "$err_msg" "$err_caller"
#  fi
#} >&2 # function writes to stderr


# Appends one or more items to an array. First parameter is array's name.
#array_append () {
#  local array=$1; shift 1
#
#  $(__bsfl_array_len len $array)
#
#  if (( len == 0 )); then
#    $(__bsfl_array_append_first $array "$1" )
#    shift 1
#  fi
#
#  local i
#  for i in "$@"; do
#    $(__bsfl_array_append $array "$i")
#  done
#}
#
#array_size () {
#  $(__bsfl_array_len size $1)
#  echo "$size"
#}
#
## Handy for debugging, print the contents of an array.
#array_print () {
#  eval "printf '%s\n' \"\${$1[@]}\""
#}
#
##  Priveate, do not use (internal to stack usage)
#declare TMP_STACK
#
#__stack_push_tmp () {
#  local TMP="$1"
#
#  if has_value TMP_STACK
#  then
#    TMP_STACK="$TMP"
#  else
#    TMP_STACK="$TMP_STACK"$'\n'"$TMP"
#  fi
#}
#
## Implementation of stack. 
## Supports push and pop (add item/get+remove item)
#
#declare STACK
#
#stack_push () {
#  line="$1"
#
#  if has_value $STACK 
#  then    
#    STACK="$line"
#  else    
#    STACK="$line"$'\n'"$STACK"
#  fi
#}
#
#stack_pop () {
#  TMP_STACK=""
#  i=0 
#  tmp=""
#  for x in $STACK
#  do
#    if [ "$i" == "0" ]
#    then
#      tmp="$x"
#    else
#      stack_push_tmp "$x"
#    fi
#    ((i++))
#  done
#  STACK="$TMP_STACK"
#  REGISTER="$tmp"
#  if [ -z "$REGISTER" ]
#  then
#    return 1
#  else
#    return 0
#  fi
#}
#
## Replace string of text in file.
## Uses the ed editor to replace the string.
##
## arg1 = string to be matched
## arg2 = new string that replaces matched string
## arg3 = file to operate on.
#str_replace_in_file () 
#{
#  local ORIG="$1"
#  local DEST="$2"
#  local FILE="$3"
#
#  has_value FILE 
#  die_if_false $? "Empty argument 'file'"
#  file_exists "$FILE"
#  die_if_false $? "File does not exist"
#
#  printf ",s/$ORIG/$DEST/g\nw\nQ" | ed -s "$FILE" > /dev/null 2>&1
#  return "$?"
#}
#
## private: don't use
#__bsfl_array_append () {
#  echo -n 'eval '
#  echo -n "$1" # array name
#  echo -n '=( "${'
#  echo -n "$1"
#  echo -n '[@]}" "'
#  echo -n "$2" # item to append
#  echo -n '" )'
#}
#
## private: don't use
#__bsfl_array_append_first () {
#  echo -n 'eval '
#  echo -n "$1" # array name
#  echo -n '=( '
#  echo -n "$2" # item to append
#  echo -n ' )'
#}
#
## private: don't use
#__bsfl_array_len () {
#  echo -n 'eval local '
#  echo -n "$1" # variable name
#  echo -n '=${#'
#  echo -n "$2" # array name
#  echo -n '[@]}'
#}
#
